package org.mc.service;

import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.mc.api.newes.ProjectServiceV1;
import org.mc.common.data.PagedDTO;
import org.mc.common.data.ResultData;
import org.mc.common.util.JSONUtils;
import org.mc.domainservice.EsDomainService;
import org.mc.request.es.ProjectRequest;
import org.mc.response.es.ProjectDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by luoxf on 2018/5/29.
 */
@RestController
@Slf4j
public class ProjectRestServiceV1 implements ProjectServiceV1 {

    @Autowired
    private EsDomainService esDomainService;

    @Override
    public ResultData<PagedDTO<List<ProjectDTO>>> getPage(@ApiParam(value = "这是当前页数")
                                                          @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                                                          @ApiParam(value = "这是每页条数")
                                                          @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {
        ResultData resultData = esDomainService.list(pageIndex, pageSize);
        log.info("page:" + JSONUtils.toJson(resultData));
        return resultData;
    }

    @Override
    public ResultData addProject(@RequestBody @Valid ProjectRequest projectRequest) {
        log.info("projectRequest:" + JSONUtils.toJson(projectRequest));
        this.esDomainService.save(JSONUtils.toJson(projectRequest));
        return ResultData.ok();
    }

    private <T> List<T> listBeanToDTO(Page page, Class<T> obj) {
        return (List<T>) page.getContent().stream().map(rs -> {
            log.info(JSONUtils.toJson(rs));
            return rs;
            //return BeanUtils.convert(rs, obj);
        }).collect(Collectors.toList());
    }
}
